import Format.JsonFormat;
import Format.RegexFormat;
import Request.HttpClient;
import bean.HotMovie;

import java.util.ArrayList;
import java.util.List;

public class SpiderRun {

    public static void main(String[] args) {
        List<HotMovie> hotMoviesList = new ArrayList<>();
        hotMoviesList = getBrief(hotMoviesList);
        getDetails(hotMoviesList);
    }

    private static void getDetails(List<HotMovie> hotMoviesList) {
        List<HotMovie> result = new ArrayList<>();
        for (int i = 0; i < hotMoviesList.size(); i++) {
            HotMovie movie = hotMoviesList.get(i);
            System.out.println("开始获取《" + movie.getTitle() + "》的详细数据.....");
            HttpClient req = new HttpClient(movie.getDetailUrl());
            RegexFormat regex = new RegexFormat(req.httpRequest());
            movie = regex.findIntroduction(movie);
            movie = regex.findDirector(movie);
            result.add(movie);
        }
        for (int i = 0; i < result.size(); i++) {
            HotMovie movie = hotMoviesList.get(i);
            System.out.println(movie.toString());
        }
    }

    private static List<HotMovie> getBrief(List<HotMovie> hotMoviesList) {
        String rootUrl = "https://movie.douban.com/j/search_subjects?type=movie&tag=热门&sort=recommend&page_limit=20&page_start=";
        for (int page = 0; page < 5; page++) {
            System.out.println("开始获取第" + (page + 1) + "页的数据.....");
            String targetUrl = rootUrl + (page * 20);
            HttpClient client = new HttpClient(targetUrl);
            JsonFormat jsonFormat = new JsonFormat(client.httpRequest());
            hotMoviesList = jsonFormat.formatJsonArray(hotMoviesList);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return hotMoviesList;
    }

}
